package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.heima.mapper.PartnerMapper;
import com.heima.pojo.Partner;
import com.heima.pojo.dto.PartnerDto;
import com.heima.pojo.dto.PartnerPageDto;
import com.heima.pojo.vo.PartnerPageVo;
import com.heima.service.PartnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class PartnerServiceImpl implements PartnerService {

    @Autowired
    private PartnerMapper partnerMapper;

    /**
     * 新增合作商
     * @param dto
     */
    @Override
    public void addPartner(PartnerDto dto) {
        //拷贝对象
        Partner partner = new Partner();
        BeanUtils.copyProperties(dto,partner);
        //补齐基本信息
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        //调用mapper
        partnerMapper.addPartner(partner);
    }

    @Override
    public PartnerPageVo pageQuery(PartnerPageDto dto) {
        dto.check();
        Page<Partner> page = partnerMapper.pageQuery(dto);
        PartnerPageVo partnerPageVo = new PartnerPageVo();
        partnerPageVo.setPageIndex(dto.getPageIndex());
        partnerPageVo.setPageSize(dto.getPageSize());
        partnerPageVo.setTotalPage(page.getPages());
        partnerPageVo.setTotalCount(page.getTotal());
        partnerPageVo.setCurrentPageRecords(page);
        log.info("服务层数据:{}", partnerPageVo);
        return partnerPageVo;
    }

    @Override
    public Partner getInfo(Integer id) {
        return partnerMapper.getInfo(id);
    }

    @Override
    public void update(Integer id, PartnerDto dto) {
        //拷贝对象
        Partner partner = new Partner();
        BeanUtils.copyProperties(dto,partner);
        partner.setId((long)id);
        partnerMapper.update(partner);
    }

    @Override
    public void delById(Integer id) {
        partnerMapper.delById(id);
    }

    /**
     * 合作商数量
     * @return
     */
    @Override
    public Integer count() {
        return partnerMapper.count();
    }

    /**
     * 重置密码
     * @param id
     */
    @Override
    public void resetPassword(Integer id) {
      partnerMapper.resetPassword(id);
    }
}
